#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：config.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/21 上午8:58 
"""

import configparser
from pathlib import Path
from typing import Dict, List


class ConfigManager:
	def __init__(self, config_path: Path):
		self.config = configparser.ConfigParser()
		self.config.read(config_path)
		self.validate()

	def validate(self):
		"""验证配置文件有效性"""
		if not self.config.has_section("global"):
			raise ValueError("Missing [global] section in config")

		required_global = ["check_interval", "max_workers"]
		for param in required_global:
			if not self.config.has_option("global", param):
				raise ValueError(f"Missing {param} in [global] section")

	@property
	def directories(self) -> List[Dict]:
		"""获取目录配置"""
		dirs = []
		for section in self.config.sections():
			if section.startswith("directory:"):
				dirs.append({
					"path": self.config.get(section, "path"),
					"max_files": self.config.getint(section, "max_files"),
					"max_size_gb": self.config.getfloat(section, "max_size_gb"),
					"policies": self.config.get(section, "policies").split(",")
				})
		return dirs

	@classmethod
	def load_default(cls):
		"""加载默认配置"""
		default_path = Path(__file__).parent.parent / "config.ini"
		return cls(default_path)
